﻿using huqiang.Framework;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class UnZipStep : AssetsAction
{
    public override void Reset()
    {
        base.Reset();
    }
    public override void Update()
    {
        

    }
    public void CheckUnzip(string section, List<KeyValue> filter)
    {
        var assets = AssetsVersionControl.NetVersion.ini.FindSection(section);
        var lzma = AssetsVersionControl.AssetsRecord.ini.FindSection("LZMA");
        if (assets != null)
        {
            if (lzma == null)
            {
                for (int i = 0; i < assets.Values.Count; i++)
                {
                    var kv = assets.Values[i];
                    for (int j = 0; j < filter.Count; j++)
                    {
                        if (filter[j].key == kv.key)
                        {
                            goto label;
                        }
                    }
                    string filepath = LocalSetting.RecordPath+ "/" + kv.key;
                    LZMAFileManager.Decode(filepath, kv.key, kv.value, "LZMA", LocalSetting.AssetsPath, DecompressDone);
                    label:;
                }
            }
            else
            {
                for (int i = 0; i < assets.Values.Count; i++)
                {
                    var kv = assets.Values[i];
                    for (int j = 0; j < filter.Count; j++)
                    {
                        if (filter[j].key == kv.key)
                        {
                            goto label;
                        }
                    }
                    string v = lzma.GetValue(kv.key);
                    if (v != kv.value)
                    {
                        string filepath = LocalSetting.RecordPath + "/" + kv.key;
                        LZMAFileManager.Decode(filepath, kv.key, kv.value, "LZMA", LocalSetting.AssetsPath, DecompressDone);
                    }
                    label:;
                }
            }
        }
    }

    public void DecompressDone(IMission mission)
    {
        AssetsVersionControl.AssetsRecord.SetString(mission.Block, mission.Name, mission.Version);
    }
    public override string Tips { get {

            var m = LZMAFileManager.GetCurrentMission();
            if (m != null)
            {
               string str = "正在解压源包:" + m.Name;
                str = str + "   " + (int)(m.Progress * 100) + "%";
                return str;
            }
            return "";
        }
    }
    public override float Progress { get {
            var m = LZMAFileManager.GetCurrentMission();
            if (m != null)
                return m.Progress;
            return 0;
        } }
}